<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AdminMember;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdminMemberController extends AdminController
{
    protected $title = '用户';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make(new AdminMember(), function (Grid $grid) {
            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                $actions->disableDelete();
//                $actions->disableEdit();
                $actions->disableQuickEdit();
                $actions->disableView();
            });
            $grid->column('member_id','ID')->sortable();
            $grid->column('member_name','用户名');
            $grid->column('phone','手机号');
//            $grid->column('password');
            $grid->column('work_company');
            $grid->column('email');
            $grid->column('post');
            $grid->column('objective');
            $grid->column('image','头像')->image('',100,100);
            $grid->column('status','状态')->switch('#333');
            $grid->column('created_at');
            $grid->column('updated_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('member_name','用户名');
                $filter->like('phone','手机号');
                $filter->like('work_company');
                $filter->like('email');
                $filter->like('post');
                $filter->like('objective');
            });
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdminMember(), function (Form $form) {
//            $form->file('file')->retainable();
//            $form->image('image')->retainable();
            $form->display('member_id','ID');
            $form->image('image','头像')->retainable()->value('http://'.$_SERVER['SERVER_NAME'].'/images/default.png');
            $form->text('member_name','用户名');
            $form->text('phone','手机号')->rules('regex:/^1[34578]\d{9}$/', [
                'regex' => '手机号格式不正确',
            ]);
            $form->password('password','密码')->default('');
            $form->text('work_company');
            $form->email('email');
            $form->text('post');
            $form->text('objective');
            $form->display('created_at');
            $form->display('updated_at');
            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = sha1($form->password);
                }
            });
            $form->saved(function (Form $form) {
                return $form->response()->success('保存成功')->redirect('admin_member');
            });
            $form->disableHeader();//去掉上面的
            $form->footer(function ($footer) {

                // 去掉`重置`按钮
                //$footer->disableReset();

                // 去掉`提交`按钮
                //$footer->disableSubmit();

                // 去掉`查看`checkbox
                $footer->disableViewCheck();

                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();

                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
        });
    }
}
